exports.__esModule = true;

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

exports.default = class extends think.BaseController {
  __before() {// 前置操作，在action执行，返回false则终止当前控制器
  }

  getUserInfoAction() {
    var _this = this;

    return _asyncToGenerator(function* () {
      // 获取个人资料
      const aj = think.ajaxJson();
      const data = {
        id: _this.get('userid')
      };
      const UserService = think.service('sys/UserService');
      const res = yield UserService.getUserInfo(data);
      if (res && !res.error) {
        aj.result = res;
      } else {
        aj.success = false;
        aj.msg = '查询失败';
      }
      return _this.json(aj);
    })();
  }

  getUserListAction() {
    var _this2 = this;

    return _asyncToGenerator(function* () {
      // 获取所有用户列表
      const aj = think.ajaxJson();
      const data = _this2.get('page,pageSize,options');
      const UserService = think.service('sys/UserService');
      const res = yield UserService.getUserList(data);
      if (!res.error) {
        aj.result = res;
      } else {
        aj.success = false;
        aj.msg = '查询失败';
      }
      return _this2.json(aj);
    })();
  }

  saveUserInfoAction() {
    var _this3 = this;

    return _asyncToGenerator(function* () {
      // 保存用户信息
      const file = _this3.file('image');
      const postData = _this3.post();
      const oldPath = _this3.post('pic_path') || null;
      const aj = think.ajaxJson();
      let saveData = {};
      const flag = _this3.post('flag');
      const userData = _this3.getUser();
      switch (Number(flag)) {
        case 1:
          if (_this3.post('id')) {
            saveData = _this3.post('id,realname,org_id,status,gender,phone,job_no,email,remarks');
          } else {
            saveData = _this3.post('username,password,realname,org_id,status,gender,phone,job_no,email,pic_path,remarks');
            saveData.create_date = new Date();
            saveData.create_by = userData.userId;
          }
          break;
        case 2:
          saveData = _this3.post('id,login_flag,update_by');
          break;
        case 3:
          saveData = _this3.post('id,username,password,update_by');
          break;
        default:
          break;
      }
      saveData.update_date = new Date();
      saveData.update_by = userData.userId;
      if (saveData.password) {
        // 密码加密
        saveData.password = think.irreversibleEncrypt(saveData.username, saveData.password);
      }
      if (_this3.post('delImgInfo')) {
        // 删除原图
        const delArr = [oldPath + '.100x100.png', oldPath + '.250x250.png'];
        const del = yield _this3.delFile(delArr);
        if (!del) {
          aj.success = false;
          aj.msg = '原有头像删除错误';
          return _this3.json(aj);
        }
        saveData.pic_path = null;
      }
      if (file) {
        // 保存图片
        const pathObj = yield _this3.saveFile(file);
        if (pathObj) {
          saveData.pic_path = pathObj.filePathStatic;
          const cutimg = yield _this3.cutImg(pathObj.filePath);
          if (!cutimg) {
            aj.success = false;
            aj.msg = '图片裁切错误';
            return _this3.json(aj);
          }
        } else {
          aj.success = false;
          aj.msg = '图片保存错误';
          return _this3.json(aj);
        }
      }

      for (var i in saveData) {
        // 更正数据格式
        if (saveData[i] === '' || saveData[i] === 'null' || saveData[i] === 'undefined') {
          saveData[i] = null;
        }
      }
      const UserService = think.service('sys/UserService');
      const res = yield UserService.saveUserInfo(saveData);
      aj.result = res;
      if (res && res.error) {
        aj.success = false;
        aj.msg = '用户信息保存失败';
      }
      if (!res) {
        aj.success = false;
        aj.msg = '用户名或手机号码已存在，请重新输入';
      }
      return _this3.json(aj);
    })();
  }

  getUserNameAction() {
    var _this4 = this;

    return _asyncToGenerator(function* () {
      // 获取用户名列表
      const aj = think.ajaxJson();
      const UserService = think.service('sys/UserService');
      const res = yield UserService.getUserName();
      if (res.error) {
        aj.success = false;
        aj.msg = '获取用户列表失败';
      } else {
        aj.result = res;
      }
      return _this4.json(aj);
    })();
  }

  delUserInfoAction() {
    var _this5 = this;

    return _asyncToGenerator(function* () {
      // 删除用户
      const aj = think.ajaxJson();
      const delData = {
        id: _this5.post('id')
      };
      const UserService = think.service('sys/UserService');
      const res = yield UserService.delUserInfo(delData);
      if (res.error) {
        aj.success = false;
        aj.msg = '删除用户信息失败';
      }
      if (!res) {
        aj.success = false;
        aj.msg = '系统用户不可删除';
      }
      return _this5.json(aj);
    })();
  }

  checkPsdAction() {
    var _this6 = this;

    return _asyncToGenerator(function* () {
      // 验证旧密码
      const aj = think.ajaxJson();
      const data = _this6.get('id,username,password');
      const UserService = think.service('sys/UserService');
      const res = yield UserService.checkPsd(data);
      if (!res) {
        aj.success = false;
        aj.msg = '旧密码输入错误';
      }
      if (res.error) {
        aj.success = false;
        aj.msg = '密码验证失败';
      }
      return _this6.json(aj);
    })();
  }
  /** *******角色关联操作用户 start************* */
  /** *******角色关联操作用户 end************* */
  saveRoleUserAction() {
    var _this7 = this;

    return _asyncToGenerator(function* () {
      // 保存用户角色关联
      const aj = think.ajaxJson();
      const data = _this7.post('userid,delList,addList');
      const UserService = _this7.service('sys/UserService');
      const res = yield UserService.saveRoleUser(data);
      if (!res) {
        aj.success = false;
        aj.msg = '用户角色关联错误';
      }
      if (res.error) {
        aj.success = false;
        aj.msg = '保存用户角色关联失败';
      }
      return _this7.json(aj);
    })();
  }

  getUserInfoByIdAction() {
    var _this8 = this;

    return _asyncToGenerator(function* () {
      const aj = think.ajaxJson();
      const UserService = think.service('sys/UserService');
      const getData = {
        id: _this8.get('id')
      };
      const res = yield UserService.getUserInfoById(getData);
      if (res.error) {
        aj.success = false;
        aj.msg = '获取用户详情失败';
      } else {
        aj.result = res;
      }
      return _this8.json(aj);
    })();
  }

  getUserInfoByIdsAction() {
    var _this9 = this;

    return _asyncToGenerator(function* () {
      const aj = think.ajaxJson();
      const UserService = think.service('sys/UserService');
      const getData = {
        ids: _this9.post('ids')
      };
      console.log('ids:', getData);
      const res = yield UserService.getUserInfoByIds(getData);
      if (res.error) {
        aj.success = false;
        aj.msg = '获取失败';
      } else {
        aj.result = res;
      }
      return _this9.json(aj);
    })();
  }

  // 获取所有用户列表
  getAllUserAction() {
    var _this10 = this;

    return _asyncToGenerator(function* () {
      const aj = think.ajaxJson();
      const UserService = think.service('sys/UserService');
      const res = yield UserService.getAllUser();
      if (res && !res.error) {
        aj.result = res;
      } else {
        aj.success = false;
        aj.msg = '查询失败';
      }
      return _this10.json(aj);
    })();
  }
  __after() {// 后置操作,在action调用之后执行

  }
  __call() {// 魔术函数

  }
};
;